package com.smartpi.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.smartpi.pojo.Common;

/**
 * Servlet implementation class SersorAnalogServlet
 */

public class SensorEnvServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SensorEnvServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String waterTemp = PythonExecuteor
				.executePython(Common.COMMAND_WATER_TEMP);
		String roomTempAndRh = PythonExecuteor
				.executePython(Common.COMMAND_ROOM_TEMP_AND_RH);
		String soilRh = PythonExecuteor.executePython(Common.COMMAND_SOIL_RH);
		String[] roomValues = roomTempAndRh.split(",");
		JSONObject json = new JSONObject();
		json.put("waterTemperature", waterTemp);
		json.put("roomTemperature", roomValues[0]);
		json.put("roomRh", roomValues[1]);
		json.put("soilRh", soilRh);
		response.getWriter().println(json.toString());
		response.getWriter().close();
	}

}
